package hlp.hibernateUtils;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateUtil {
	

	    private static SessionFactory sessionFactory;

	    private static void createSessionFactory(){
	        try {
	            // Create the SessionFactory
	            sessionFactory = new AnnotationConfiguration().configure("/hlp/hibernateUtils/hibernate.cfg.xml").buildSessionFactory();
	        } catch (Throwable ex) {
	            // Make sure you log the exception, as it might be swallowed
	            System.err.println("Initial SessionFactory creation failed." + ex);
	            throw new ExceptionInInitializerError(ex);
	        }
	    }

	    
	    public static Session currentSession() throws HibernateException {
	    	if (sessionFactory==null || sessionFactory.isClosed()){
	    		createSessionFactory();
	    	}
	    	return sessionFactory.getCurrentSession();
	    }

	    public static void closeSession() throws HibernateException {
	        
	    }

}
